import { _decorator, Node, director, view, screen, Vec3 } from 'cc';
import { CommonUI } from "./CommonUI";
import GYLoader from '../Core/资源加载/GYLoader';
import { UIBase } from './UIBase';

export class UIManager {
    public static instance: UIManager = null;

    // UI层级管理
    private uiRoot: Node = null;
    private normalLayer: Node = null;
    private popupLayer: Node = null;
    private loadingLayer: Node = null;
    private toastLayer: Node = null;

    // 资源管理
    private abName: string = "GYPrefabs";
    private abBundle: any = null;
    private hasInited: boolean = false;

    private constructor() { }

    public static getInstance(): UIManager {
        if (!this.instance) {
            this.instance = new UIManager();
        }
        return this.instance;
    }

    public adjustUILayer() {
        const canvas = director.getScene()?.getChildByName('Canvas');
        if (!canvas || !this.uiRoot) {
            return;
        }
        
        // 获取Canvas下所有子节点的数量
        const childCount = canvas.children.length;
        // 将GuyuUIRoot设置为最高层级
        this.uiRoot.setSiblingIndex(childCount - 1);
    }

    private initUILayers() {
        // 获取Canvas节点并创建UI根节点
        const canvas = director.getScene().getChildByName('Canvas');
        if (!canvas) {
            console.error('Canvas节点不存在！');
            return;
        }
        
        this.uiRoot = new Node('GuyuUIRoot');
        canvas.addChild(this.uiRoot);
        this.uiRoot.layer = 1 << 25; // UI层级
        this.uiRoot.setSiblingIndex(1000); // 确保UI层在游戏层之上
        
        // 设置UI根节点位置
        this.uiRoot.setPosition(new Vec3(0, 0, 0));

        // 创建各个层级节点
        this.normalLayer = new Node('NormalLayer');
        this.popupLayer = new Node('PopupLayer');
        this.loadingLayer = new Node('LoadingLayer');
        this.toastLayer = new Node('ToastLayer');

        // 设置层级关系
        this.uiRoot.addChild(this.normalLayer);
        this.uiRoot.addChild(this.popupLayer);
        this.uiRoot.addChild(this.loadingLayer);
        this.uiRoot.addChild(this.toastLayer);

        // 设置所有UI层级的位置
        const layers = [this.normalLayer, this.popupLayer, this.loadingLayer, this.toastLayer];
        layers.forEach(layer => {
            layer.setPosition(new Vec3(0, 0, 0));
        });

        // 设置层级顺序
        this.normalLayer.setSiblingIndex(0);
        this.popupLayer.setSiblingIndex(100);
        this.loadingLayer.setSiblingIndex(200);
        this.toastLayer.setSiblingIndex(300);
        UIBase.printNodeHierarchy();
    }

    public async init(): Promise<boolean> {
        if (this.hasInited) {
            return true;
        }
        this.hasInited = true;

        // 初始化UI层级
        this.initUILayers();

        // 加载资源包
        let res = await GYLoader.loadBundle(this.abName);
        if (!res.bundle) {
            console.error(`UIManager, 加载资源失败:`, res);
            return false;
        }
        this.abBundle = res.bundle;
        await CommonUI.init();
        return true;
    }

    // 获取各层级节点
    public getNormalLayer(): Node { return this.normalLayer; }
    public getPopupLayer(): Node { return this.popupLayer; }
    public getLoadingLayer(): Node { return this.loadingLayer; }
    public getToastLayer(): Node { return this.toastLayer; }
    public getUIRoot(): Node { return this.uiRoot; }
    public getBundle(): any { return this.abBundle; }

    public addChildTo(node: Node, parent: "normal" | "popup" | "loading" | "toast") {
        switch (parent) {
            case "normal":
                this.normalLayer.addChild(node);
                break;
            case "popup":
                this.popupLayer.addChild(node);
                break;
            case "loading":
                this.loadingLayer.addChild(node);
                break;
            case "toast":
                this.toastLayer.addChild(node);
                break;
            default:
                this.normalLayer.addChild(node);
                break;
        }
    }

    public findChild(node: Node, parent: "normal" | "popup" | "loading" | "toast"): Node {
        if (!node) return null;
        let parentNode: Node;
        switch (parent) {
            case "normal":
                parentNode = this.normalLayer;
                break;
            case "popup":
                parentNode = this.popupLayer;
                break;
            case "loading":
                parentNode = this.loadingLayer;
                break;
            case "toast":
                parentNode = this.toastLayer;
                break;
            default:
                parentNode = this.normalLayer;
                break;
        }
        return parentNode?.children.find(child => child === node);
    }
}